import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import ChatView from '../views/ChatView.vue'
import ZhuceView from '../views/ZhuceView.vue'
import WangjiView from '../views/WangjiView.vue'
import HomePage from '../views/HomePage.vue'
import ProgramView from '../views/ProgramView.vue' 
import PersonView from '../views/PersonView.vue' 
import TeamView from '../views/TeamView.vue' 

const routes = [
  {
    path: '/',
    redirect: '/HomePage'
  },
  {
    path: '/ChatView',
    name: 'ChatView',
    component: ChatView,
    meta: { 
      requiresAuth: true, 
      title: '汤圆AI',
      keepAlive: true 
    }
  },
  {
    path: '/LoginView',
    name: 'LoginView',
    component: LoginView
  },
   {
    path: '/ZhuceView',
    name: 'ZhuceView',
    component:ZhuceView
  },
   {
    path: '/WangjiView',
    name: 'WangjiView',
    component:WangjiView
  },
   {
    path: '/HomePage',
    name: 'HomePage',
    component:HomePage
  },
  {
    path: '/ProgramView',
    name: 'ProgramView',
    component: ProgramView
  },
  {
    path: '/PersonView',
    name: 'PersonView',
    component: PersonView
  },
  {
    path: '/TeamView',
    name: 'TeamView',
    component: TeamView
  }
]
const router = createRouter({
  history: createWebHistory(),
  routes
});
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const user = sessionStorage.getItem('user')
    if (!user) {
      if (from.path !== '/') {
        next({
          path: '/LoginView',
          query: { redirect: to.fullPath } 
        })
      } 
      else {
        next()
      }
      return
    }
  }
  next()
})

export default router;